//弹框控制
$(function() {
	$(document).ready(function(e) {
        $(".pop-click").click(function(){
        	var login_in = $(".sitenav_right .pl10");
        	if(login_in[0] == undefined){
        		$("#dialog-pay").fadeIn(500);
        		$("#kaptcha").attr("src","/kaptcha.jpg?" + Math.floor(Math.random() * 100000000));
        	}
		});
		$(".popup-close").click(function(){
			$("#dialog-pay").fadeOut(500);
			_czc.push(["_trackPageview","/button.login.close"]);
			});
        $(".zj-click").click(function(){
        	var login_in = $(".sitenav_right .pl10");
        	if(login_in[0] == undefined){
        		$("#dialog-pay").fadeIn(500);
        		$("#kaptcha").attr("src","/kaptcha.jpg?" + Math.floor(Math.random() * 100000000));
        	}else{  //加载投票信息，并显示弹框
        		loadVote();
        	}
		_czc.push(["_trackPageview","/button.vote"]);
			});
		$(".zj-close").click(function(){
			$("#dialog-vote").fadeOut(500);
			$(".vote_ico").attr("style", "display:block;");
			_czc.push(["_trackPageview","/button.vote.close"]);
		});
		$(".line-close").click(function(){
			$("#dialog-line").fadeOut(500);
		});
    });
});
//加载投票信息
function loadVote(){
	$.ajax({
		type:"get",
		url:"/live/voteinfo.html?t=" + Math.random(),
		success:function(result){
			var errCode = result.errCode;
			switch(errCode){
			case 0:
				var str = "";
				str += "<h5 class=\"mb10\"><span style=\"color:#666;\" class=\"b\">"+result.voteType.name+"</span>&#12288;(有<em style=\"color:#106594;\">"+result.voteNum+"</em>人已投票)</h5>";
				for(var i=0;i<result.votes.length;++i){
					str += "<p><a href=\"javascript:vote("+result.voteType.id+","+(i+1)+");\">";
					if(i==0) str += "选项一 [ ";
					else if(i==1) str += "选项二 [ ";
					else if(i==2) str += "选项三 [ ";
					else if(i==3) str += "选项四 [ ";
					str += result.votes[i].content + " ]</a></p>";
				}
				$("#dialog-vote .login_box .vote").html(str);
				$("#dialog-vote").fadeIn(500);
				$(".vote_ico").attr("style", "display:none;");
				break;
			case 1:
				$("#dialog-pay").fadeIn(500);
				break;
			case 2:
				$("#dialog-pay").fadeIn(500);
				break;
			case 3:
				alert(result.errMsg);
				break;
			}
		},
		error:function(){
			alert("系统服务无响应，请稍后再试!");
		}
	});
}
//投票
function vote(voteType,value){
	$.ajax({
		type:"get",
		url:"/live/vote.html?votetype="+ voteType + "&value=" + value + "&t=" + Math.random(),
		async:true,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success:function(data){
			var errCode=data.errCode;
			if (errCode == 0) {
				$("#dialog-vote .login_box .vote").attr("style","display:none;")
				$("#dialog-vote .login_box .vote_ok").attr("style","display:block;")
				if(data.hasVote==1)
					$(".vote_ico").attr("style", "display:block;");
			}else{
				alert("投票失败，请稍后重试！");
			}	
		},
		error:function(){
			alert("系统服务无响应，请稍后再试!");
		}
	});
}
//投票成功后，确认，关闭弹框
$(function(){
	$("#te_vote").click(function(){
		$("#dialog-vote").fadeOut(10);
		$("#dialog-vote .login_box .vote_ok").attr("style","display:none;")
		$("#dialog-vote .login_box .vote").attr("style","display:block;")
		_czc.push(["_trackPageview","/button.vote.ok"]);
	});
})
//刷新验证码
function changeKaptcha(){
	$("#kaptcha").attr("src","/kaptcha.jpg?" + Math.floor(Math.random() * 100000000));
	$("#code").focus();
}
//弹框登录
$(function(){
	$('#te_1').click(function(){
		userLogin();
		_czc.push(["_trackPageview","/button.login"]);
	});
});
//提交，最终验证
function userLogin(){
	if(verify()){
		var auto=0;
		if($('#autoLogin').is(':checked')==true){auto=1;}
		var data = {username:$("#name").val(),password:$("#password").val(),verify:$("#code").val(),auto:auto};
		$.ajax({
			type:"post",
			url:"/member/login.html",
			data:data,
			success:function(data){
				var errCode = data.errCode;
				switch(errCode){
					case 0:
						gLoginFlag = true;
						window.location.reload();
						break;
					case 1:
						alert(data.errMsg);
						changeKaptcha();
						$("#name").val("");
						$("#name").focus();
						break;
					case 2:
						alert(data.errMsg);
						changeKaptcha();
						$("#password").val("");
						$("#password").focus();
						break;
					case 3:
						alert(data.errMsg);
						changeKaptcha();
						break;
					case 4:
						alert(data.errMsg);
						changeKaptcha();
						break;
				}
			},
			error: function(){
				//请求出错处理
				alert("系统服务无响应，请稍后再试!");
			}, 
		});
	}
}
function verify(){
	if($("#name").val() == "" || $("#name").val() == "请输入用户名"){
		alert("请输入用户名！");
		return false;
	}
	if($("#password").val() == "" || $("#password").val() == "请输入密码"){
		alert("请输入登录密码！");
		return false;
	}
	if($("#code").val() == "" || $("#code").val() == "验证码"){
		alert("请输入验证码！");
		return false;
	}
	return true;
}
